package com.soxing.braindata.service.sync;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soxing.braindata.dao.entity.Testee;
import com.soxing.braindata.dao.entity.UserInfo;
import com.soxing.braindata.dao.service.ITesteeService;
import com.soxing.braindata.domain.enumeration.FunTypeEnum;
import com.soxing.braindata.domain.response.sync.SyncResponse;
import com.soxing.braindata.domain.rpc.OriginReserve;
import com.soxing.braindata.domain.rpc.OriginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 同步访视预约
 */
@Slf4j
@Service("syncReserveService")
public class SyncReserveServiceImpl implements ISyncService<OriginReserve> {

    @Resource
    private ITesteeService testeeService;

    @Override
    public SyncResponse syncData(OriginReserve originReserve) {
        try{
            Integer funType = originReserve.getFunType();
            String innerCode = originReserve.getInnerCode();
            if(funType==null||innerCode==null)
            {
                log.error("funType或innerCode为空。funType:{},innerCode:{}", funType, innerCode);
                return SyncResponse.fail("funType或innerCode为空");
            }
            sync(originReserve);

            return SyncResponse.success();
        }catch (Exception e)
        {
            log.error("同步被试预约服务异常", e);
            return SyncResponse.fail();

        }
    }


    public void sync(OriginReserve originReserve)
    {
        Testee testee = createTestee(originReserve);
        testeeService.remove(new LambdaQueryWrapper<Testee>().eq(Testee::getTesteeNo,testee.getTesteeNo()));
        if(originReserve.getFunType()==FunTypeEnum.DELETE.getFunType())
        {
            return;
        }
        testeeService.save(testee);
    }


    private Testee createTestee(OriginReserve originReserve)
    {
        Testee testee = new Testee();
        testee.setTesteeNo(originReserve.getInnerCode());
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd");
        try {
            testee.setBirthday(sdf.parse(originReserve.getDateOfBirth()));
        } catch (ParseException e) {
            testee.setBirthday(new Date());
            throw new RuntimeException(e);
        }
        testee.setName(originReserve.getPatientName());
        testee.setSitePkId(originReserve.getSitePkId());
        testee.setSiteCode(originReserve.getSiteCode());
        testee.setSex(originReserve.getSex().equals("男")?1:0);
        testee.setSubjectPkId(originReserve.getSubjectPkId());
        testee.setSubjectCode(originReserve.getSubjectCode());
        if(originReserve.getType()==null){
            testee.setType(0);
        }else{
            testee.setType(originReserve.getType());
        }
        return testee;
    }



}
